package com.leosoft.heatmonitor.service.transactional;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.leosoft.heatmonitor.dao.ProjectDAO;
import com.leosoft.heatmonitor.dao.UserDAO;
import com.leosoft.heatmonitor.model.User;

@Component("projectService")
public class ProjectService {
	@Autowired
	private ProjectDAO projectDAO;
	@Autowired
	private UserDAO userDAO;

	public Map<String, Object> list(int startIndex, int length,
			List<String[]> wherePart, List<String[]> odrerPart, String username) {
		User user = userDAO.getUserByName(username);
		if (!user.getRoles().isEmpty()) {
			if (user.getRoles().get(0).getRole().equals("PM")) {
				return projectDAO.list(startIndex, length, wherePart,
						odrerPart, user.getId());
			} else {
				return projectDAO.list(startIndex, length, wherePart,
						odrerPart, "");
			}
		}

		return null;
	}

	public boolean create(String name, String description, String province,
			String city, String district, Double rate) {
		if (projectDAO.notExist(name, province, city, district)) {
			projectDAO.create(name, description, province, city, district, rate);
			return true;
		}
		return false;
	}

	public void update(String id, String name, String description,
			String province, String city, String district, Double rate) {
		projectDAO
				.update(id, name, description, province, city, district, rate);

	}

	public void delete(String id) {
		projectDAO.delete(id);
	}

	public List<Map<String, Object>> loadProject(String username) {
		User user = userDAO.getUserByName(username);
		if (!user.getRoles().isEmpty()) {
			if (user.getRoles().get(0).getRole().equals("PM")) {
				return projectDAO.loadProject(user.getId());
			} else {
				return projectDAO.loadProject("");
			}
		}
		return null;
	}

	public List<String> suggestProjectName(String projectName, String username) {
		User user = userDAO.getUserByName(username);
		if (!user.getRoles().isEmpty()) {
			if (user.getRoles().get(0).getRole().equals("PM")) {
				return projectDAO.suggestProject(projectName, user.getId());
			} else {
				return projectDAO.suggestProject(projectName, "");
			}
		}
		return null;
	}

	public Object dashBoard(String username) {
		User user = userDAO.getUserByName(username);
		if (!user.getRoles().isEmpty()) {
			if (user.getRoles().get(0).getRole().equals("PM")) {
				return projectDAO.dashBoard(user.getId());
			} else {
				return projectDAO.dashBoard("");
			}
		}
		return null;
	}
}
